package com.nttdata.ta.user.controller;

import com.nttdata.ta.common.dto.ResponseResult;
import com.nttdata.ta.common.dto.UserDTO;
import com.nttdata.ta.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/users")
public class UserController {
    
    private final UserService userService;
    
    @GetMapping("/{id}")
    public ResponseEntity<ResponseResult<UserDTO>> getUserById(@PathVariable Long id) {
        try {
            UserDTO userDTO = userService.getUserById(id);
            return ResponseEntity.ok(ResponseResult.success(userDTO));
        } catch (Exception e) {
            log.error("获取用户失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail(e.getMessage()));
        }
    }
    
    @GetMapping
    public ResponseEntity<ResponseResult<List<UserDTO>>> getAllUsers() {
        try {
            List<UserDTO> users = userService.getAllUsers();
            return ResponseEntity.ok(ResponseResult.success(users));
        } catch (Exception e) {
            log.error("获取所有用户失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail(e.getMessage()));
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<ResponseResult<UserDTO>> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        try {
            UserDTO updatedUser = userService.updateUser(id, userDTO);
            return ResponseEntity.ok(ResponseResult.success("用户更新成功", updatedUser));
        } catch (Exception e) {
            log.error("更新用户失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail(e.getMessage()));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<ResponseResult<Void>> deleteUser(@PathVariable Long id) {
        try {
            userService.deleteUser(id);
            return ResponseEntity.ok(ResponseResult.success("用户删除成功", null));
        } catch (Exception e) {
            log.error("删除用户失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail(e.getMessage()));
        }
    }
    
    @PostMapping("/validate")
    public ResponseEntity<ResponseResult<Boolean>> validateToken(@RequestHeader("Authorization") String authHeader) {
        try {
            String token = authHeader.substring(7); // 去掉"Bearer "前缀
            boolean isValid = userService.validateToken(token);
            return ResponseEntity.ok(ResponseResult.success(isValid));
        } catch (Exception e) {
            log.error("令牌验证失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail("令牌验证失败"));
        }
    }
    
    @GetMapping("/id-from-token")
    public ResponseEntity<ResponseResult<Long>> getUserIdFromToken(@RequestHeader("Authorization") String authHeader) {
        try {
            String token = authHeader.substring(7); // 去掉"Bearer "前缀
            Long userId = userService.getUserIdFromToken(token);
            return ResponseEntity.ok(ResponseResult.success(userId));
        } catch (Exception e) {
            log.error("从令牌获取用户ID失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail(e.getMessage()));
        }
    }
} 